package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.nle.mylibrary.enums.led.PlayType;
import com.nle.mylibrary.enums.led.ShowSpeed;
import com.nle.mylibrary.forUse.led.LedScreen;
import com.nle.mylibrary.forUse.mdbus4150.MdBus4150RelayListener;
import com.nle.mylibrary.forUse.mdbus4150.Modbus4150;
import com.nle.mylibrary.forUse.rfid.RFID;
import com.nle.mylibrary.forUse.rfid.SingleEpcListener;
import com.nle.mylibrary.transfer.DataBusFactory;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private RFID rfid;
    private TextView tv1, tv2, tv3, tv4, tv5, tv6;
    private LedScreen ledScreen;
    private Modbus4150 modbus4150;
    private Timer timer;
    private TimerTask timerTask;
    private Integer count=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        timer=new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                try {
                    Log.i("main","abc");
                    rfid.readSingleEpc(new SingleEpcListener() {
                        @Override
                        public void onVal(String val) {
                            if (count <= 6) {
                                if (tv1.getText().toString().equals("") && !save.get(val, MainActivity.this)) {
                                    save.save(val,MainActivity.this);
                                    count+=1;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Log.i("abc","芜湖");
                                            tv1.setText(val);
                                            tv1.setBackgroundResource(R.drawable.bcd);
                                        }

                                    });
                                }else if(tv2.getText().toString().equals("")&&!save.get(val, MainActivity.this)){
                                    save.save(val,MainActivity.this);
                                    count+=1;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {

                                            tv2.setText(val);
                                            tv2.setBackgroundResource(R.drawable.bcd);
                                        }
                                    });
                                }else if(tv3.getText().toString().equals("")&&!save.get(val, MainActivity.this)){
                                    save.save(val,MainActivity.this);
                                    count+=1;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            tv3.setText(val);
                                            tv3.setBackgroundResource(R.drawable.bcd);
                                        }
                                    });
                                }
                                else if(tv4.getText().toString().equals("")&&!save.get(val, MainActivity.this)){
                                    save.save(val,MainActivity.this);
                                    count+=1;

                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            tv4.setText(val);
                                            tv4.setBackgroundResource(R.drawable.bcd);
                                        }
                                    });
                                }
                                else if(tv5.getText().toString().equals("")&&!save.get(val,MainActivity.this)){
                                    save.save(val,MainActivity.this);
                                    count+=1;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            tv5.setText(val);
                                            tv5.setBackgroundResource(R.drawable.bcd);
                                        }
                                    });
                                } else if(tv6.getText().toString().equals("")&&!save.get(val, MainActivity.this)){
                                    save.save(val,MainActivity.this);
                                    count+=1;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            tv6.setText(val);
                                            tv6.setBackgroundResource(R.drawable.bcd);
                                        }
                                    });
                                }
                                if(save.get(val,MainActivity.this)){
                                    try {
                                        modbus4150.openRelay(7, new MdBus4150RelayListener() {
                                            @Override
                                            public void onCtrl(boolean isSuccess) {
                                                Log.i("main",isSuccess+"");
                                            }
                                        });
                                        ledScreen.sendTxt("此票据已登记",PlayType.LEFT,ShowSpeed.SPEED1,99,100);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }else {
                                    try {
                                        modbus4150.closeRelay(7, new MdBus4150RelayListener() {
                                            @Override
                                            public void onCtrl(boolean isSuccess) {
                                                Log.i("main",isSuccess+"");
                                            }
                                        });
                                        ledScreen.sendTxt("aaa",PlayType.LEFT,ShowSpeed.SPEED1,99,100);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }else {
                                try {
                                    ledScreen.sendTxt("满了",PlayType.LEFT,ShowSpeed.SPEED1,99,100);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        timer.schedule(timerTask,1,2000);

    }
    private void init() {
        rfid = new RFID(DataBusFactory.newSocketDataBus("172.21.8.202", 4196));
        ledScreen = new LedScreen(DataBusFactory.newSocketDataBus("172.21.8.203", 4196));
        modbus4150 = new Modbus4150(DataBusFactory.newSocketDataBus("172.21.8.201", 4196));
        tv1 = findViewById(R.id.desk1);
        tv2 = findViewById(R.id.desk2);
        tv3 = findViewById(R.id.desk3);
        tv4 = findViewById(R.id.desk4);
        tv5 = findViewById(R.id.desk5);
        tv6 = findViewById(R.id.desk6);

    }


}

